package com.lee.dhsg.service.impl;

import com.lee.dhsg.dao.TeacherDao;
import com.lee.dhsg.domain.Teacher;
import com.lee.dhsg.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该服务层接口用于实现教师相关的业务
 * 1、查询推荐教师
 * 2、教师登录
 * 3、教师注册
 *
 * @author Lee
 */

@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    /**
     * 查询推荐教师
     * 通过字段 tuijian 为 1 ，查询
     * @return
     */
    @Override
    public List<Teacher> selectByTuijian() {

        Teacher teacher = new Teacher();
        teacher.setTuijian(1);
        Map<String, Object> params = new HashMap<>();
        params.put("teacher", teacher);
        List<Teacher> teachers = teacherDao.selectByPage(params);

        return teachers;

    }

    /**
     * 教师登录
     * 通过用户名和密码查询 teacher 表，返回一个 Teacher 对象
     * @param username
     * @param password
     * @return
     */
    @Override
    public Teacher login(String username, String password) {
        return teacherDao.selectByLoginnameAndPassword(username,password);
    }

    /**
     * 教师注册
     * 往 teacher 里添加一条数据，该条数据最初只有用户名和密码
     * @param username
     * @param password
     * @throws Exception
     */
    @Override
    public void register(String username, String password) throws Exception {
        try {
            teacherDao.Signup(username,password);
        }catch (Exception e){
            throw new Exception("注册失败"+e.getMessage());
        }
    }
}
